// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  traceUser: true,
  env: 'aaa111-9gh9fp5ieac4de5d'
})

const db = cloud.database()

const time_format = (time) => {
  if (time.toString().length == 13) {
    var tme = new Date(time);
  } else {
    var tme = new Date(time * 1000);
  }
  var Y = tme.getFullYear();
  var M = (tme.getMonth() + 1 < 10 ? '0' + (tme.getMonth() + 1) : tme.getMonth() + 1);
  var D = tme.getDate();
  var h = tme.getHours();
  var m = tme.getMinutes();
  var tem1 = Y + '-' + M + '-' + D + ' ' + h + ':' + m;
//  console.log(tem1);
  return tem1;
}

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  var postinfo = await db.collection('postinfo').where({
    _id: event._id
  }).get()
  postinfo = postinfo.data[0] //获得文章信息
  //获取评论信息
  var commentCount = await db.collection("comment").where({
    artId: event._id
  }).count()
  var commentListRaw = await db.collection("comment").where({
    artId: event._id
  }).limit(2).get()
  var result = []
  for (var i = 0; i < commentListRaw.data.length; i++) {
    var temp = {}
    var userInfo = await db.collection("user").where({
      _openid: commentListRaw.data[i]._openid
    }).get();
//    console.log(commentListRaw.data[i])
  //  console.log(userInfo.data[0])
    temp = {
      commentInfo: commentListRaw.data[i],
      userInfo: userInfo.data[0],
      time: time_format(commentListRaw.data[i].timestamp)
    }
    result.push(temp)
  }

  //获取发帖人信息
  var postopenid = postinfo._openid
  var postuserinfo = await db.collection('user').where({
    _openid: postopenid
  }).get()
  postuserinfo = postuserinfo.data[0] //或者发文人信息
  //console.log(postuserinfo)

  //发帖人是否是自己
  var notMe = true
  if (postopenid == wxContext.OPENID) {
    notMe = false
  } else {
    notMe = true
  }

  //是否关注博主
  var isfollow = await db.collection('follow').where({
    _openid: wxContext.OPENID,
    follow: postopenid
  }).get()
  var unfollow = true
  if (isfollow.data.length == 1) {
    unfollow = false
  } else if (isfollow.data.length == 0) {
    unfollow = true
  }

  //获取用户点赞收藏信息

  var userHeart = await db.collection("userHeart").where({
    _openid: wxContext.OPENID,
    artId: event._id
  }).count()

  var userStar = await db.collection("userStar").where({
    _openid: wxContext.OPENID,
    artId: event._id
  }).count()

  //获取文章点赞收藏数量

  var artHeart = await db.collection("userHeart").where({
    artId: event._id
  }).count()

  var artStar = await db.collection("userStar").where({
    artId: event._id
  }).count()

  //记录阅读用户和时间
  await db.collection("postReadHistory").add({
    data:{
      _openid: wxContext.OPENID,
      artId: event._id,
      time: new Date().getTime()
    }
  }).then(res=>{
    console.log(res)
  })

  return {
    writerName: postuserinfo.username,
    writerImage: postuserinfo.userimage,
    images: postinfo.image,
    maintext: postinfo.intro,
    time: time_format(postinfo.timestamp),
    unfollow,
    notMe,
    comment: result,
    commentCount,
    userCtrl: {
      userHeart: userHeart.total,
      userStar: userStar.total
    },
    artCtrl: {
      artHeart: artHeart.total,
      artStar: artStar.total,
    }
  }
}